package cn.xinyangtong.manager.service.impl;

import cn.xinyangtong.common.pojo.DataResult;
import cn.xinyangtong.common.pojo.DefaultResult;
import cn.xinyangtong.common.util.ConstantsUtil;
import cn.xinyangtong.manager.dao.JedisClient;
import cn.xinyangtong.manager.service.ContentService;
import cn.xinyangtong.mapper.TbContentMapper;
import cn.xinyangtong.pojo.TbContent;
import cn.xinyangtong.pojo.TbContentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 内容管理
 * Created by fengbo on 2017/6/17.
 */
@Service("contentService")
public class ContentServiceImpl implements ContentService {

    private final Logger log = LoggerFactory.getLogger(ContentServiceImpl.class);

    @Autowired
    private TbContentMapper contentMapper;
    @Autowired
    private JedisClient jedisClient;

    //添加内容
    @Override
    public DefaultResult insertContent(TbContent content) {
        //补全pojo
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);
        long id = contentMapper.insert(content);
        if (id > 0) {
            syncContent(content.getCategoryId());
            return DefaultResult.ok();
        }
        return  DefaultResult.error();
    }

    //根据分类id查询内容
    @Override
    public DataResult getContentList(Long categoryId, Integer page, Integer rows) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(page, rows);
        List<TbContent> list = contentMapper.selectByExample(example);
        DataResult result = new DataResult();
        result.setRows(list);
        //取出总记录数
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    //根据IDs删除内容
    @Override
    public DefaultResult deleteByIds(Long[] ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(ids));
        int num = contentMapper.deleteByExample(example);
        if (num <= 0) {
            log.error("Delete Content Fail! ids" + ids.toString());
            return DefaultResult.error();
        }
        return DefaultResult.ok();
    }

    //修改内容
    @Override
    public DefaultResult updateContent(TbContent content) {
        int num = contentMapper.updateByPrimaryKeySelective(content);
        if (num <= 0) {
            log.error("Update Content Fail! ContentId = " + content.getId());
            return DefaultResult.error();
        }
        syncContent(content.getCategoryId());
        return DefaultResult.ok();
    }

    /***
     * 首页大广告位缓存同步
     * @param contentCid
     */
    private void syncContent(Long contentCid) {
        try {
            jedisClient.hdel(ConstantsUtil.INDEX_CONTENT_REDIS_KEY, contentCid.toString());
        } catch (Exception e) {
            log.error("同步首页大广告位缓存失败！" + e.getMessage());
        }
    }
}
